package com.hyp.service.impl;

import com.hyp.mapper.FavoritesMapper;
import com.hyp.pojo.Favorites;
import com.hyp.pojo.Product;
import com.hyp.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FavoritesServicelmpl  implements FavoritesService {
    @Autowired
    private FavoritesMapper favoritesMapper;
    @Override
    public void toggleFavorite(Integer userId,Integer productId, boolean isFavorite) {
        Favorites favorites = favoritesMapper.findByUserIdAndProductId(userId, productId);
        if (isFavorite) {
            if (favorites == null) {
                favorites = new Favorites(null, userId, productId, LocalDateTime.now());
                favoritesMapper.insert(favorites);
            }
        } else {
            if (favorites != null) {
                favoritesMapper.deleteById(favorites.getFavoriteID());
            }
        }
    }

    @Override
    public boolean isFavorite(Integer userId, Integer productId) {
        Favorites favorites = favoritesMapper.findByUserIdAndProductId(userId, productId);
        return favorites != null;
    }

    @Override
    public List<Product> favoriteList(Integer userId) {
        return favoritesMapper.favoriteList(userId);
    }
}
